package com.javalong.affairs.module.renwujinzhang.activity.submit;

import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.reflect.TypeToken;
import com.javalong.affairs.R;
import com.javalong.affairs.base.common.ViewHolder;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.listener.MultipartRequest;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.multirequest.MultipartRequestParams;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.IntUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.javalong.affairs.module.renwujinzhang.activity.RecycleListActivity;
import com.javalong.affairs.module.renwujinzhang.model.Task;
import com.javalong.affairs.module.renwujinzhang.ui.SelectFileView;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class TaskSubmitActivity extends RecycleListActivity {

    private Task task;

    public static void toThisActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, TaskSubmitActivity.class);
        context.startActivity(intent);
    }

    public static void toThisActivity(Context context, Task task) {
        Intent intent = new Intent();
        intent.setClass(context, TaskSubmitActivity.class);
        intent.putExtra(BundleKey.TASK, task);
        context.startActivity(intent);
    }

    @Override
    public void afterInit() {
        getSupportActionBar().setTitle("任务提交");
        task = (Task) getIntent().getSerializableExtra(BundleKey.TASK);
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        switch (position) {
            case 0: {
                EditText editText = holder.getView(R.id.et_right);
            }
            break;
            case 1: {
                ((SelectFileView) holder.itemView).setHintText("选择文件");
            }
            break;
            case 2: {
                Button button = (Button) holder.itemView;
                button.setText("保存");
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        save();
                    }
                });
            }
            break;
            case 3: {
                Button button = (Button) holder.itemView;
                button.setText("提交");
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        submit();
                    }
                });
            }
            break;
        }
    }

    private void submit() {
        String url = UrlUtils.getUrl("Task", "action_submit");
        File currFile = (File) getValue(1);
        String content = (String) getValue(0);
        request(url, currFile, content);
    }

    private void save() {
        String url = UrlUtils.getUrl("Task", "action_submit_save");
        File currFile = (File) getValue(1);
        String content = (String) getValue(0);
        request(url, currFile, content);
    }

    private void request(String url, File currFile, String content) {
        if (currFile != null) {
            MultipartRequestParams param = new MultipartRequestParams();
            param.put(RequestKey.ID, task.getId() + "");
            param.put(RequestKey.RECEIVEATTACHMENT, currFile);
            param.put(RequestKey.RECEIVECONTENT, content);
            HttpUtils.addReuqest(new MultipartRequest(url, param,
                    new BaseResponseListener(new TypeToken<User>() {
                    }.getType()) {
                        @Override
                        public void onSuccess(Object obj) {
                            super.onSuccess(obj);
                            finish();
                        }
                    }));
        } else {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put(RequestKey.ID, task.getId() + "");
            param.put(RequestKey.RECEIVECONTENT, content);
            HttpUtils.post(url, param, new BaseResponseListener(
                    new TypeToken<User>() {
                    }.getType()) {
                @Override
                public void onSuccess(Object obj) {
                    super.onSuccess(obj);
                    finish();
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return EDIT;
            case 1:
                return FILE;
        }
        return 0;
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_task_submit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_submit:
                submit();
                break;
        }
        return true;
    }
}
